using System;
using System.Collections;
using System.Collections.Generic;
using Server.Mobiles;
using Server.Items;
using Server.Gumps;
using Server.Network;


namespace Server.Gumps
{
	public class PetInfoGump : Gump
	{
		public static readonly int GumpOffsetX = PropsConfig.GumpOffsetX;
		public static readonly int GumpOffsetY = PropsConfig.GumpOffsetY;

		public static readonly int TextHue = PropsConfig.TextHue;
		public static readonly int TextOffsetX = PropsConfig.TextOffsetX;

		public static readonly int OffsetGumpID = PropsConfig.OffsetGumpID;
		public static readonly int HeaderGumpID = PropsConfig.HeaderGumpID;
		public static readonly int 	EntryGumpID = PropsConfig.EntryGumpID;
		public static readonly int   BackGumpID = PropsConfig.BackGumpID;
		public static readonly int    SetGumpID = PropsConfig.SetGumpID;

		public static readonly int SetWidth = PropsConfig.SetWidth;
		public static readonly int SetOffsetX = PropsConfig.SetOffsetX, SetOffsetY = PropsConfig.SetOffsetY;
		public static readonly int SetButtonID1 = PropsConfig.SetButtonID1;
		public static readonly int SetButtonID2 = PropsConfig.SetButtonID2;

		public static readonly int PrevWidth = PropsConfig.PrevWidth;
		public static readonly int NextWidth = PropsConfig.NextWidth;

		public static readonly int PrevOffsetX = PropsConfig.PrevOffsetX, PrevOffsetY = PropsConfig.PrevOffsetY;
		public static readonly int PrevButtonID1 = PropsConfig.PrevButtonID1;
		public static readonly int PrevButtonID2 = PropsConfig.PrevButtonID2;

		public static readonly int NextOffsetX = PropsConfig.NextOffsetX, NextOffsetY = PropsConfig.NextOffsetY;
		public static readonly int NextButtonID1 = PropsConfig.NextButtonID1;
		public static readonly int NextButtonID2 = PropsConfig.NextButtonID2;

		public static readonly int OffsetSize = PropsConfig.OffsetSize;

		public static readonly int EntryHeight = PropsConfig.EntryHeight;
		public static readonly int BorderSize = PropsConfig.BorderSize;

		private static readonly int HeaderTextHue = 777;
		private static readonly int ValueTextHue = 1159;

		private static readonly int WidthName = 120;
		private static readonly int WidthValue = 160;

		private static readonly int NameWidth = SetWidth;
		private static readonly int ValueWidth = WidthName + BorderSize + WidthValue;
		private static readonly int EntryCount = 15;

		private static readonly int TotalWidth = OffsetSize + NameWidth + OffsetSize + ValueWidth + OffsetSize + SetWidth + OffsetSize;
		private static readonly int TotalHeight = OffsetSize + ((EntryHeight + OffsetSize) * (EntryCount + 1));

		private static readonly int BackWidth = BorderSize + TotalWidth + BorderSize;

		private int m_Page;
		private List<PetProp> m_pp;

		public PetInfoGump( List<PetProp> list, int page ) : base( GumpOffsetX, GumpOffsetY )
		{
			m_Page = page;
			m_pp = list;

			if ( m_pp == null || !( m_pp.Count > 0 ) )
				return;

			Initialize( page );
		}

		private void Initialize( int page )
		{
			int count = m_pp.Count - (page * EntryCount);

			if ( count < 0 )
				count = 0;
			else if ( count > EntryCount )
				count = EntryCount;

			int lastIndex = (page * EntryCount) + count - 1;

			if ( lastIndex >= 0 && lastIndex < m_pp.Count && m_pp[lastIndex] == null )
				--count;

			int totalHeight = OffsetSize + ((EntryHeight + OffsetSize) * (count + 1));

			AddPage( 0 );

			AddBackground( 0, 0, BackWidth, BorderSize + totalHeight + BorderSize, BackGumpID );
			AddImageTiled( BorderSize, BorderSize, TotalWidth, totalHeight, OffsetGumpID );

			int x = BorderSize + OffsetSize;
			int y = BorderSize + OffsetSize;

			int emptyWidth = TotalWidth - PrevWidth - NextWidth - (OffsetSize * 4);
			AddImageTiled( x, y, PrevWidth, EntryHeight, HeaderGumpID );


			if ( page > 0 )
			{
				AddButton( x + PrevOffsetX, y + PrevOffsetY, PrevButtonID1, PrevButtonID2, 1, GumpButtonType.Reply, 0 );
			}

			x += PrevWidth + OffsetSize;
			AddImageTiled( x, y, emptyWidth, EntryHeight, HeaderGumpID );

			x += NameWidth - PrevWidth + OffsetSize;
			AddLabelCropped( x + TextOffsetX, y, WidthName - TextOffsetX, EntryHeight, HeaderTextHue, "Property" );
			x += WidthName + BorderSize;
			AddLabelCropped( x + TextOffsetX, y, WidthValue - TextOffsetX, EntryHeight, HeaderTextHue, "Value" );
			x += WidthValue + OffsetSize;


//			x += emptyWidth + OffsetSize;
			AddImageTiled( x, y, NextWidth, EntryHeight, HeaderGumpID );

			if ( (page + 1) * EntryCount < m_pp.Count )
			{
				AddButton( x + NextOffsetX, y + NextOffsetY, NextButtonID1, NextButtonID2, 2, GumpButtonType.Reply, 1 );
			}

			for ( int i = 0, index = page * EntryCount; i < count && index < m_pp.Count; ++i, ++index )
			{
				x = BorderSize + OffsetSize;
				y += EntryHeight + OffsetSize;


				PetProp pp = m_pp[index];

				AddImageTiled( x, y, NameWidth, EntryHeight, EntryGumpID );
				AddLabelCropped( x + TextOffsetX, y, NameWidth - TextOffsetX, EntryHeight, TextHue, "" );
				x += NameWidth + OffsetSize;
				AddImageTiled( x, y, ValueWidth, EntryHeight, EntryGumpID );
				AddLabelCropped( x + TextOffsetX, y, WidthName - TextOffsetX, EntryHeight, TextHue, pp.m_name );
				x += WidthName + BorderSize;
				AddLabelCropped( x + TextOffsetX, y, WidthValue - TextOffsetX, EntryHeight, ValueTextHue, pp.m_value );
				x += WidthValue + OffsetSize;


				if ( SetGumpID != 0 )
					AddImageTiled( x, y, SetWidth, EntryHeight, SetGumpID );
			}
		}

		public override void OnResponse( NetState state, RelayInfo info )
		{
			Mobile from = state.Mobile;
			
			switch ( info.ButtonID )
			{
				case 0: // Closed
				{
					break;
				}
				case 1: // Previous
				{
					if ( m_Page > 0 )
						from.SendGump( new PetInfoGump( m_pp, m_Page - 1 ) );

					break;
				}
				case 2: // Next
				{
					if ( ( m_Page + 1 ) * EntryCount < m_pp.Count )
						from.SendGump( new PetInfoGump( m_pp, m_Page + 1 ) );

					break;
				}
				default:
				{

					break;
				}
			}
		}
	}
}